﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Windows.Media;
using MobiSms.Infrastructure;

namespace MobiSms.DataModel
{
    public class OptionInfo: NotificationObject
    {
        public OptionInfo()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                CheckSettings();
            }
        }

        #region Fields
        private string _backgroundImage = "/MobiSms.Wasun;component/Assets/grass.png";
        private string _language = "zh-CN";
        private Stretch _backgroundImageStretch = Stretch.UniformToFill;
        private bool _showRecentBirthday = true;
        private int _notifyDays = 7;
        #endregion

        #region Properties
        public string BackgroundImage
        {
            get { return _backgroundImage; }
            set
            {
                _backgroundImage = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ImageName] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => BackgroundImage);
            }
        }

        public string Language
        {
            get { return _language; }
            set
            {
                _language = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.Language] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => Language);
            }
        }

        public bool ShowRecentBirthday
        {
            get
            {
                return _showRecentBirthday;
            }
            set
            {
                _showRecentBirthday = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ShowRecentBirthdayPerson] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => ShowRecentBirthday);
            }
        }

        public Stretch BackgroundImageStretch
        {
            get { return _backgroundImageStretch; }
            set
            {
                _backgroundImageStretch = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.ImageStretch] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => BackgroundImageStretch);
            }
        }

        public int NotifyDays
        {
            get { return _notifyDays; }
            set
            {
                _notifyDays = value;
                IsolatedStorageSettings.ApplicationSettings[OptionKeys.NotifyDays] = value;
                IsolatedStorageSettings.ApplicationSettings.Save();
                RaisePropertyChanged(() => NotifyDays);
            }
        }

        public IEnumerable<Stretch> StretchCollection
        {
            get
            {
                yield return Stretch.Fill;
                yield return Stretch.None;
                yield return Stretch.Uniform;
                yield return Stretch.UniformToFill;
            }
        }
        #endregion

        #region Methods
        private void CheckSettings()
        {
            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            #region Background
            if (!settings.Contains(OptionKeys.ImageName))
            {
                settings.Add(OptionKeys.ImageName, _backgroundImage);
                settings.Save();
            }
            else
            {
                BackgroundImage = settings[OptionKeys.ImageName].ToString();
            }
            #endregion

            #region Language
            if (!settings.Contains(OptionKeys.Language))
            {
                settings.Add(OptionKeys.Language, _language);
                settings.Save();
            }
            else
            {
                Language = settings[OptionKeys.Language].ToString();
            }
            #endregion

            #region ShowRecentBirthday
            if (!settings.Contains(OptionKeys.ShowRecentBirthdayPerson))
            {
                settings.Add(OptionKeys.ShowRecentBirthdayPerson, _showRecentBirthday);
                settings.Save();
            }
            else
            {
                string _value = settings[OptionKeys.ShowRecentBirthdayPerson].ToString();
                ShowRecentBirthday = bool.Parse(_value);
            }
            #endregion

            #region BackgroundImageStretch
            if (!settings.Contains(OptionKeys.ImageStretch))
            {
                settings.Add(OptionKeys.ImageStretch, _backgroundImageStretch);
                settings.Save();
            }
            else
            {
                BackgroundImageStretch = (Stretch)settings[OptionKeys.ImageStretch];
            }
            #endregion

            #region NotifyDays
            if (!settings.Contains(OptionKeys.NotifyDays))
            {
                settings.Add(OptionKeys.NotifyDays, _notifyDays);
                settings.Save();
            }
            else
            {
                NotifyDays = Int32.Parse(settings[OptionKeys.NotifyDays].ToString());
            }
            #endregion
        }
        #endregion

        #region Event
        public event EventHandler<PropertyArgs> SettingsChanged = delegate { };

        protected override void RaisePropertyChanged(string propertyName)
        {
            base.RaisePropertyChanged(propertyName);
            SettingsChanged.Invoke(this, new PropertyArgs()
            {
                Property = GetType().GetProperty(propertyName),
                Value = GetType().GetProperty(propertyName).GetValue(this, null)
            });
        }

        public void Reset()
        {
            IsolatedStorageSettings.ApplicationSettings.Clear();
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ShowRecentBirthdayPerson, true);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ImageName, "/MobiSms.Wasun;component/Assets/grass.png");
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.ImageStretch, Stretch.UniformToFill);
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.Language, "zh-CN");
            IsolatedStorageSettings.ApplicationSettings.Add(OptionKeys.NotifyDays, 7);
            IsolatedStorageSettings.ApplicationSettings.Save();

            CheckSettings();
        }

        public void Save()
        {
            IsolatedStorageSettings.ApplicationSettings.Save();
        }
        #endregion
    }
}
